package org.jaaksi.customviewdemo

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.TimeInterpolator
import android.animation.ValueAnimator
import android.os.Bundle
import android.view.animation.Animation
import android.view.animation.ScaleAnimation
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_test.*

class TestActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)

//        val displayMetrics = resources.displayMetrics
//        println(">> displayMetrics = $displayMetrics")
//        // 这个决定加载那个values资源文件夹下的资源，取小于swdp的，如411会取400，450取440
//        val swdp = 160f * displayMetrics.widthPixels / displayMetrics.densityDpi
//        textView.text = "swdp = $swdp\n$displayMetrics"

        container.setOnClickListener {
            container.post {
                // 320 1.28f 0.8f
                // 280 0.8f 1.02f
                // 200f 1.02f 0.98f
                // 200f 1f

                val valuesArr = arrayOf(1.28f, 0.8f, 1.02f, 0.98f, 1f)
                val timeArr = arrayOf<Long>(320, 280, 200, 200)
                timeArr.forEachIndexed { index, duration ->
                    ValueAnimator.ofFloat(valuesArr[index], valuesArr[index + 1]).apply {
                        this.duration = duration
                        addUpdateListener {
                            container.scaleX = it.animatedValue as Float
                            container.scaleY = it.animatedValue as Float
                        }
                        startDelay = getDelayTime(timeArr, index)
                        start()
                    }
                }
//                AnimatorSet().apply {
//                    playTogether()
//
//                }

            }
        }
    }

    private fun getDelayTime(timeArr: Array<Long>, position: Int): Long {
        if (position == 0) return 0
        return timeArr.filterIndexed { index, l -> index < position }.sum()
    }

//    private fun test() {
//        val all = assets.open("all2.json").bufferedReader().use { it.readText() }
//        val url = assets.open("url.json").bufferedReader().use { it.readText() }
//        val type = object : TypeToken<Map<String, String>>() {}.type
//
//        tryRun {
//            val allData = Gson().fromJson<Map<String, String>>(all, type)
//            val urlData = Gson().fromJson<Map<String, String>>(url, type)
//            val not = allData.filterNot { urlData.containsKey(it.key) }
//            Timber.d(not.map { it.key }.toJson())
//            // ["150","639","641","719","720"]
//            val filter = urlData.filter { allData.containsKey(it.key) }
//            val toJson = filter.toJson()
//
//        }
//    }

//    inner class MyInterpolator : TimeInterpolator {
//        override fun getInterpolation(input: Float): Float {
//            return when{
//                input <= 1/21f -> {
//                    1f - input
//                }
//            }
//        }
//
//    }
}